<?php
namespace app\admin\controller;
/**
 * 公告管理
 */
class Notice extends Common
{
    private $model;
    public function __construct()
    {
        parent::__construct();
        $this -> model = db('notice');
    }
	public function index()
	{
		if(request()->isPost())
		{
			$key=input('post.key');
            $list = $this -> model  ->where('Title','like',"%".$key."%") ->order('DateTime')->select();
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list,'rel'=>1];
        }
		return view();
	} 
 
	public function add()
	{
		if(request()->isPost())
		{
            $data = input('post.');
			$data["DateTime"]=date('Y-m-d h:i:s', time());
            $info = db('notice') -> insert($data);
            if ($info)
            {
            	$result['msg'] = '添加成功!';
	            $result['url'] = url('index');
	            $result['code'] = 1;
            } else 
            {
            	$result['msg'] = '添加失败!';
	            $result['url'] = url('add');
	            $result['code'] = 0;
            }
            return $result;
        }
		return view();
	}

    public function edit()
    {
        if(request()->isPost()) 
        {
            $data = input('post.');
            $this -> model -> update($data);
            $result['msg'] = '修改成功!';
            $result['url'] = url('index');
            $result['code'] = 1;
            return $result;
        } 

        $id   = input('param.id');
        $list = $this -> model -> find($id);
		//var_dump($list);
        $this->assign('info',$list);
        return view();
    }

    public function del()
    {
        $id = input('param.id');
        if (empty($id))
        {
            return ['code'=>0,'msg'=>'ID不存在！'];
        }
        $this -> model ->delete($id);
        return ['code'=>1,'msg'=>'删除成功！'];
		
    }

    public function order()
    {
        $data = input('post.');
        $this -> model ->update($data);
        $result['msg'] = '排序更新成功!';
        $result['url'] = url('index');
        $result['code'] = 1;
        return $result;
    }

    public function status()
    {
        $id=input('post.id');
        $status=$this->model->where(array('id'=>$id))->value('status');
        if($status==1)
        {
            $data['status'] = 0;
            $this->model->where(array('id'=>$id))->setField($data);
            $result['is_lock'] = 0;
            $result['status']  = 1;
        }else
        {
            $data['status'] = 1;
            $this->model->where(array('id'=>$id))->setField($data);
            $result['is_lock'] = 1;
            $result['status'] = 1;
        }
        return $result;
    }
}